package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/*
* 客户国际授信系统
* */
public class YNSDI003GJAction extends BaseBean implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName = "formtable_main_" + formidAbs;

        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("客户国际授信系统=======mainTableInfoMap:" + com.alibaba.fastjson.JSONObject.toJSONString(mainTableInfoMap));


        try {

            JSONObject result = new JSONObject();
            JSONArray data = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("PARTNER",mainTableInfoMap.get("khbm"));
            jsonObject.put("RISK_CLASS",mainTableInfoMap.get("fxl"));
            jsonObject.put("CHECK_RULE",mainTableInfoMap.get("xyjcgz"));
            jsonObject.put("CREDIT_SGMNT",mainTableInfoMap.get("xyd"));
            jsonObject.put("CREDIT_LIMIT",mainTableInfoMap.get("cwpfed"));
            jsonObject.put("LIMIT_VALID_DATE",mainTableInfoMap.get("yxzzrq"));

            data.add(jsonObject);
            result.put("data",data);

            writeLog("客户授信系统=======result:" + result.toString());
            String post = YNHttpUtils.SDI003(result.toString(), YNHttpUtils.token());
            writeLog("客户授信系统=======post:" + post);

            JSONObject jsonObject1 = JSONObject.parseObject(post);

            JSONArray aReturn = jsonObject1.getJSONArray("RETURN");
            JSONObject jsonObject2 = aReturn.getJSONObject(0);
            String status = jsonObject2.getString("STATUS");
            String message = jsonObject2.getString("MESSAGE");
            //String partner = jsonObject2.getString("PARTNER");

            //更新主表qqcs和fhjg
            RecordSet recordSet = new RecordSet();
            String updateMainTableName = "update " + tableName + " set  qqcsx = ? , fhjgx = ?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, result.toString(), post, requestid);


            if (!"S".equals(status)) {
                requestInfo.getRequestManager()
                        .setMessageid(requestInfo.getRequestid()+"OA-MMI030物料主数据创建接口");
                requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:"+message);
                return Action.FAILURE_AND_CONTINUE;
            }


        }
        catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid()+"OA-MMI030物料主数据创建接口");
            requestInfo.getRequestManager().setMessagecontent("接口异常"+e.toString());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;


    }
}
